package com.cumt.threeway.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.cumt.threeway.R;
import com.cumt.threeway.adapter.StrategyAdapter;
import com.cumt.threeway.constant.ThreeWayConstant;
import com.cumt.threeway.constant.URLS;
import com.cumt.threeway.model.Strategy;
import com.cumt.threeway.table.GonglueEntity;
import com.cumt.threeway.util.ThreeWayData;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 胡晶晶 on 2016/5/24.
 */
public class ActivitiesStrategyActivity extends FragmentActivity implements View.OnClickListener,AdapterView.OnItemClickListener{

    protected ListView mListView;
    protected List<Strategy> mList;
    private List<GonglueEntity> mGongLueList;
    private RequestQueue queue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_strategy_layout);
        initView();
//        getDataFromServer();
        getData();
    }

    private void getDataFromServer() {
        queue = Volley.newRequestQueue(this);
        String url = URLS.IP + "NetServlet/SelectGonglue";
        // 集合==》json数组
        final Gson g = new Gson();
        JsonArrayRequest jarsingle;
        jarsingle = new JsonArrayRequest(Request.Method.POST, url, null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                String data = response.toString();
                mList = g.fromJson(data, new TypeToken<List<Strategy>>() {}.getType());
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {
            }
        });
        queue.add(jarsingle);
    }

    private void getData() {
        mGongLueList = ThreeWayData.getInstance().getGongLueInfo();
        StrategyAdapter adapter = new StrategyAdapter(mGongLueList, ActivitiesStrategyActivity.this);
        mListView.setAdapter(adapter);
    }

    protected void initView() {
        queue = Volley.newRequestQueue(this);
        mList = new ArrayList<Strategy>();
        mListView = (ListView) findViewById(R.id.lv);
        mListView.setOnItemClickListener(this);
        findViewById(R.id.btn_back).setOnClickListener(this);
        findViewById(R.id.tv_write).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.tv_write:
                goWriteStrategyActivity();
                break;
        }
    }

    private void goWriteStrategyActivity() {
        Intent intent = new Intent(ActivitiesStrategyActivity.this, WriteStrategyActivity.class);
        startActivityForResult(intent, ThreeWayConstant.INTENT_KEY_TO_LOGIN_1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK) {
//            getDataFromServer();
            getData();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        GonglueEntity strategy = mGongLueList.get(position);
        Intent intent = new Intent(ActivitiesStrategyActivity.this, StrategyInfoActivity.class);
        intent.putExtra("strategy", strategy);
        intent.putExtra("flag", "all");
        startActivity(intent);
    }
}
